#include <QLinkedList>
#include <QList>
#include <QFont>
#include <QColor>
#include <QBrush>
#include <QFontMetrics>

#include "frequency_information_model.h"
#include "information.h"

Frequency_information_model::Frequency_information_model(QObject *parent)
    : QAbstractTableModel(parent)
    , informations_list_(nullptr)
{
}

int Frequency_information_model::rowCount(const QModelIndex &parent) const
{
    int count(0);

    if ( !parent.isValid() && nullptr != informations_list_) {
        count = informations_list_->count();
    }

    return count;
}

int Frequency_information_model::columnCount(const QModelIndex &parent) const
{
    int count(0);
    if ( !parent.isValid()) {
        count = 4;
    }

    return count;
}

QVariant Frequency_information_model::data(const QModelIndex &index, int role) const
{
    if ( !index.isValid()) {
        return QVariant();
    }

    QVariant var = QVariant();

    // 根据role的角色，设置var的值
    switch (role) {
    case Qt::DisplayRole: {
        Information* information(informations_list_->at(index.row()));

        // 根据具体的列号，返回数据（扇区信息、频率、是否互调、互调频率）
        switch (index.column()) {
        case 0:
            var = information->get_sector_information();
            break;

        case 1:
            var = information->get_frequency();
            break;

        case 2:
            var = information->get_is_inter_modulation();
            break;

        case 3:
            var = information->get_inter_modulation_frequency();
            break;
        }

        break;
    }

        // 文字字体
    case Qt::FontRole: {
        Information* information(informations_list_->at(index.row()));

        if (information->get_is_inter_modulation()) {
            var = QFont("Consolas", 18, QFont::Bold);
        } else {
            var = QFont("Consolas", 14, QFont::Bold);
        }

        break;
    }

        // 文字背景色
    case Qt::BackgroundRole: {
        switch (index.row() % 2) {
        case 0:
            var = QBrush(Qt::yellow, Qt::SolidPattern);
            break;

        case 1:
//            var = QBrush(QColor(135, 206, 255), Qt::SolidPattern);
            var = QBrush(Qt::green, Qt::SolidPattern);
            break;

//        case 2:
//            var = QBrush(QColor(238, 162, 173), Qt::SolidPattern);
//            break;
        }

        break;
    }

        // 文字前景色
    case Qt::ForegroundRole: {
        Information* information(informations_list_->at(index.row()));

        if (information->get_is_inter_modulation()) {
            var = QBrush(Qt::red, Qt::SolidPattern);
        } else {
            var = QBrush(Qt::black, Qt::SolidPattern);
        }

        break;
    }

    }

    return var;
}

QVariant Frequency_information_model::headerData(int section,
                                                 Qt::Orientation orientation,
                                                 int role) const
{
    QVariant var = QVariant();

    // 处理列表头，当为列表头时，直接调用父类方法
    if (Qt::Vertical == orientation) {
        return QAbstractItemModel::headerData(section, orientation, role);
    }

    // 处理行表头
    switch (role) {
    case Qt::DisplayRole: {
        switch (section) {
        case 0:
            var = tr("扇区信息");
            break;

        case 1:
            var = tr("频率(MHz)");
            break;

        case 2:
            var = tr("是否互调");
            break;

        case 3:
            var = tr("造成互调频率(MHz)");
            break;
        }

        break;
    }

    case Qt::FontRole:
        var = QFont("Courier New", 14, QFont::Bold);
        break;

    case Qt::ForegroundRole:
        var = QBrush(Qt::black, Qt::SolidPattern);
        break;

    default:
        var = QAbstractTableModel::headerData(section, orientation, role);
        break;

    }

    return var;
}

void Frequency_information_model::set_informations_list(QList<Information*>* list)
{
    beginResetModel();
    informations_list_ = list;
    endResetModel();
}

void Frequency_information_model::reset_model_data()
{
    const int count(informations_list_->count());

    for (int index(0); count != index; ++index) {
        informations_list_->at(index)->set_is_inter_modulation(false);
        informations_list_->at(index)->set_inter_modulation_frequency(0.0);
    }
}

QList<Information *> *Frequency_information_model::get_informations_list()
{
    return informations_list_;
}
